import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_channel_plugin/flutter_channel_plugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var _platformVersion = 'Unknown';
  static const MethodChannel _channel =
      const MethodChannel('flutter_channel_plugin');

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    print("initPlatformState");
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      //platformVersion = await FlutterChannelPlugin.platformVersion;
      platformVersion = await _channel.invokeMethod('getPlatformVersion');
    } on Exception catch (e) {
      platformVersion = e.toString();
      platformVersion = 'Failed to get platform version.';
    }

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion'),
              RaisedButton(
                onPressed: _pressed,
                child: Text("调用getPlatformVersion"),
              ),
              RaisedButton(
                onPressed: _pressed1,
                child: Text("postMessage(int)"),
              ),
              RaisedButton(
                onPressed: _pressed2,
                child: Text("postMessage(bool)"),
              ),
              RaisedButton(
                onPressed: _pressed3,
                child: Text("postMessage(String)"),
              ),
              RaisedButton(
                onPressed: _pressed4,
                child: Text("postMessage(List)"),
              ),
              RaisedButton(
                onPressed: _pressed5,
                child: Text("postMessage(Map)"),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _pressed() {
    initPlatformState();
  }

  void _pressed1() async {
    var platformVersion;
    try {
      platformVersion = await _channel.invokeMethod('postMessage', 1);
      print("_pressed1:" + platformVersion);
    } on Exception catch (e) {
      print(e);
      platformVersion = "Failed to get platform version. $e";
    }

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  void _pressed2() async {
    var platformVersion;
    try {
      platformVersion = await _channel.invokeMethod('postMessage', true);
      print("_pressed1:" + platformVersion);
    } on Exception catch (e) {
      print(e);
      platformVersion = "Failed to get platform version. $e";
    }

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  void _pressed3() async {
    var platformVersion;
    try {
      platformVersion = await _channel.invokeMethod('postMessage', "string");
      print("_pressed1:" + platformVersion);
    } on Exception catch (e) {
      print(e);
      platformVersion = "Failed to get platform version. $e";
    }

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  void _pressed4() async {
    var platformVersion;
    try {
      var list = [1, 2, 3, 4];
      platformVersion = await _channel.invokeMethod('postMessage', list);
      print("_pressed1:" + platformVersion);
    } on Exception catch (e) {
      print(e);
      platformVersion = "Failed to get platform version. $e";
    }

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  void _pressed5() async {
    var platformVersion;
    try {
      var map = {};
      map[1] = "a";
      map[2] = "b";
      map[3] = "c";
      platformVersion = await _channel.invokeMethod('postMessage', map);
      print("_pressed1:" + platformVersion);
    } on Exception catch (e) {
      print(e);
      platformVersion = "Failed to get platform version. $e";
    }

    setState(() {
      _platformVersion = platformVersion;
    });
  }
}
